package com.rc.evcharger.biz.notify;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.common.mq.MqConfig;
import com.rc.evcharger.common.mq.MqProducer;
import com.rc.evcharger.mapper.elec.ElecGunMapper;
import com.rc.evcharger.mapper.elec.ElecPileMapper;
import com.rc.evcharger.model.elec.ElecGun;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.vo.cec.ConnectorStatusInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 6.3 notification_stationsStatus
 * 当设备状态发生变化时，推送最新的状态到应用层
 */
@Component
public class NotiyStationsStatus {
    private static Logger logger = LogManager.getLogger(NotiyStationsStatus.class);

    @Autowired
    private MqProducer mqProducer;

    //@Autowired
    //private CmdMqProducer mqProducer;

    @Autowired
    private ElecGunMapper elecGunMapper;

    @Autowired
    private ElecPileMapper elecPileMapper;

    /**
     * 推送电枪的最新状态
     * @param elecGun
     */
    public void notifyGunStatus(ElecGun elecGun) {
        ConnectorStatusInfo connectorStatusInfo = new ConnectorStatusInfo();
        connectorStatusInfo.setConnectorID(elecGun.getGunNo());

        //获取枪最新状态
        ElecGun newElecGun = elecGunMapper.selectByElecGunId(elecGun.getElecGunId());

        //桩信息
        ElecPile elecPile = elecPileMapper.selectByPrimaryKey(elecGun.getElecPileId());

        //桩status登录状态: 0离线 1在线 默认0
        if (elecPile != null && elecPile.getStatus() != null && elecPile.getStatus() == 1) {
            //status枪使用状态 0：空闲，1：使用中  2:插枪中 默认0
            //CEC:
            //0: 离网；
            //1: 空闲；
            //2: 占用（未充电）；
            //3: 占用（充电中）；
            //4: 占用（预约锁定）；
            //255: 故障
            if (newElecGun.getStatus() != null) {
                if (newElecGun.getStatus() == 0) {//空闲
                    connectorStatusInfo.setStatus(1);
                } else if (newElecGun.getStatus() == 1) {//使用中
                    connectorStatusInfo.setStatus(3);
                } else if (newElecGun.getStatus() == 2) {  //充电前调用，插枪了并且空闲返回成功
                    connectorStatusInfo.setStatus(2);
                }
            } else {
                connectorStatusInfo.setStatus(255);
            }
        } else {
            connectorStatusInfo.setStatus(0);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String enData = null;
        try {
            enData = objectMapper.writeValueAsString(connectorStatusInfo);
        } catch (JsonProcessingException e) {
            logger.error(e);
        }

        mqProducer.send(MqConfig.CALLBACK_STATIONS_STATUS, enData);

        logger.info("notifyGunStatus推送完成，对应的枪号:{},枪状态:{}",
                connectorStatusInfo.getConnectorID(), connectorStatusInfo.getStatus());
    }

    /**
     * 推送电桩的最新状态
     * @param elecPile
     */
    public void notifyPileStatus(ElecPile elecPile) {
        //查找此桩的所有枪信息
        List<ElecGun> elecGuns = elecGunMapper.selectByElecPileId(elecPile.getElecPileId());

        //推送所有枪状态
        for (ElecGun elecGun : elecGuns) {
            notifyGunStatus(elecGun);
        }
    }
}
